有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何使用JOption窗格修复乘法表?

我正在尝试用用户输入的所需表格和他/她想要显示的数字制作一个乘法表(例如,输入2表示表格,输入5表示数字将得到2,4,6,8,10)

我已经尝试了以下方法,但它无法提供所需的输出

public class MultiplicationTable {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int num = Integer.parseInt(JOptionPane.showInputDialog("Enter a number."));

        int value = Integer.parseInt(JOptionPane.showInputDialog("Enter number of times."));

        printTable( num,  value);
    }

    private static void printTable(int num, int value) {
        int [] table = new int[value];
        for(int i = 0; i<=value-1; i++) {
            table[i] = num*i;
        }
        System.out.println(table); 
    }
}

它正在打印:[I@3d494fbf 当它应该打印乘法表时


共 (3) 个答案

  1. # 1 楼答案

    试试java.util.Arrays.toString(int[])API

    printTable()中,您可以这样打印:

    System.out.println(Arrays.toString(table));
    
  2. # 2 楼答案

    将打印行放入循环中 例如:

    for(int i = 1; i<=value-1; i++)
    {
        table[i] = num*i;
        System.out.println(table[i])
    }
    

    价值观[I@3d494fbf'您的程序正在打印的是'table'数组内存路径

  3. # 3 楼答案

    当使用System.out.println打印对象(如数组)时,实际发生的是调用对象的toString方法

    现在根据Object类的toString方法:

    The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

    getClass().getName() + '@' + Integer.toHexString(hashCode())

    如果要打印数组的内容,请尝试System.out.println(Arrays.toString(table));